林嶔 (Lin, Chin)
Lesson 13
允許使用者上傳檔案進行分析是一個統計軟體必備的條件,在shiny app裡面,我們將透過「fileInput」讓使用者上傳檔案。
而R裡面要讀取檔案,主要是利用read.table()
– 請在這裡下載練習用檔案
Data=read.table("Example.txt",header=TRUE) #讀取Example.txt,並存成"Data"這個物件(資料表格式)
head(Data,10) #看"Data"這個資料表的前10個row
## Height Weight BMI Cholesterol Triglyceride
## 1 173.9095 69.30906 22.91623 196.8222 146.4670
## 2 171.1519 66.72796 22.77951 183.2731 152.1301
## 3 169.4500 69.74043 24.28855 193.6936 150.3699
## 4 164.4632 58.76008 21.72426 182.4610 140.6752
## 5 172.2093 64.85015 21.86743 186.5315 146.7873
## 6 166.4883 66.17726 23.87489 187.4913 165.1633
## 7 177.2201 92.62094 29.49057 211.4545 167.2411
## 8 177.6544 85.45003 27.07448 203.5222 166.3448
## 9 175.4248 89.43701 29.06267 208.3781 164.0171
## 10 163.2245 55.52598 20.84133 185.0684 149.5531
summary(Data) #看"Data"這個資料表的所有變項的基本資訊
## Height Weight BMI Cholesterol
## Min. :143.6 Min. : 35.15 Min. :13.66 Min. :161.7
## 1st Qu.:162.9 1st Qu.: 63.47 1st Qu.:22.54 1st Qu.:187.2
## Median :170.6 Median : 69.75 Median :24.62 Median :197.6
## Mean :168.7 Mean : 70.83 Mean :24.75 Mean :196.2
## 3rd Qu.:173.7 3rd Qu.: 80.53 3rd Qu.:27.21 3rd Qu.:204.8
## Max. :197.5 Max. :103.20 Max. :35.32 Max. :219.6
## Triglyceride
## Min. :114.0
## 1st Qu.:149.7
## Median :158.6
## Mean :158.6
## 3rd Qu.:167.0
## Max. :194.9
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Summarize your data"),
sidebarPanel(
fileInput(inputId="files", label=h4("Upload your data file:"), multiple=FALSE, accept="text/plain"),
helpText("Note: you only can upload the .txt file."),
sliderInput("n", h4("Select the observed rows:"), min=5, max=20, value=10)
),
mainPanel(
verbatimTextOutput("summary"),
tableOutput("view")
)
))
library(shiny)
shinyServer(function(input, output) {
output$summary = renderPrint({
if (is.null(input$files)==TRUE) {return("You have to up load your data!!!")}
if (is.null(input$files)==FALSE) {
dat <- read.table(input$files$datapath,header=T)
summary(dat)
}
})
output$view = renderTable({
if (is.null(input$files)==TRUE) {return()}
if (is.null(input$files)==FALSE) {
dat <- read.table(input$files$datapath,header=T)
head(dat,input$n)
}
})
})
– radioButtons() in ui.R(單選題)
– plotOutput() in ui.R(輸出圖形)
– renderPlot() in server.R(處理圖形)
Data=read.table("Example.txt",header=TRUE) #讀取Example.txt,並存成"Data"這個物件(資料表格式)
head(Data,10) #看"Data"這個資料表的前10個row
## Height Weight BMI Cholesterol Triglyceride
## 1 173.9095 69.30906 22.91623 196.8222 146.4670
## 2 171.1519 66.72796 22.77951 183.2731 152.1301
## 3 169.4500 69.74043 24.28855 193.6936 150.3699
## 4 164.4632 58.76008 21.72426 182.4610 140.6752
## 5 172.2093 64.85015 21.86743 186.5315 146.7873
## 6 166.4883 66.17726 23.87489 187.4913 165.1633
## 7 177.2201 92.62094 29.49057 211.4545 167.2411
## 8 177.6544 85.45003 27.07448 203.5222 166.3448
## 9 175.4248 89.43701 29.06267 208.3781 164.0171
## 10 163.2245 55.52598 20.84133 185.0684 149.5531
summary(Data) #看"Data"這個資料表的所有變項的基本資訊
## Height Weight BMI Cholesterol
## Min. :143.6 Min. : 35.15 Min. :13.66 Min. :161.7
## 1st Qu.:162.9 1st Qu.: 63.47 1st Qu.:22.54 1st Qu.:187.2
## Median :170.6 Median : 69.75 Median :24.62 Median :197.6
## Mean :168.7 Mean : 70.83 Mean :24.75 Mean :196.2
## 3rd Qu.:173.7 3rd Qu.: 80.53 3rd Qu.:27.21 3rd Qu.:204.8
## Max. :197.5 Max. :103.20 Max. :35.32 Max. :219.6
## Triglyceride
## Min. :114.0
## 1st Qu.:149.7
## Median :158.6
## Mean :158.6
## 3rd Qu.:167.0
## Max. :194.9
Color="red"
plot(Data,col=Color)
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Summarize your data"),
sidebarPanel(
fileInput(inputId="files", label=h4("Upload your data file:"), multiple=FALSE, accept="text/plain"),
helpText("Note: you only can upload the .txt file."),
sliderInput("n", h4("Select the observed rows:"), min=5, max=20, value=10),
radioButtons("Color", "Select the color of histogram:", choices = c("Red" = "red", "Blue" = "blue", "Green" = "green"))
),
mainPanel(
verbatimTextOutput("summary"),
plotOutput("scatterPlot"),
tableOutput("view")
)
))
library(shiny)
shinyServer(function(input, output) {
DATA <- reactive({
if (is.null(input$files)) {return()} else {
dat <- read.table(input$files$datapath,header=T)
return(dat)
}
})
output$summary = renderPrint({
dat = DATA()
if (is.null(dat)) {return("You have to up load your data!!!")} else {
summary(dat)
}
})
output$scatterPlot = renderPlot({
dat = DATA()
if (is.null(dat)) {return()} else {
plot(dat,col=input$Color)
}
})
output$view = renderTable({
dat = DATA()
if (is.null(dat)) {return()} else {
head(dat,input$n)
}
})
})
## Functions Inputs
## 1 checkboxGroupInput() selected values
## 2 checkboxInput() logical
## 3 dateInput() date
## 4 dateRangeInput() 2 dates
## 5 fileInput() file name
## 6 numericInput() numeric
## 7 radioButtons() selected value
## 8 selectInput() selected value(s)
## 9 sliderInput() numeric
## 10 submitButton() numeric
## 11 textInput() character
– 請在這裡下載基本物件範例
請在這裡下載練習用檔案
現在,請寫出一個App,功能如下描述:
Data=read.table("Example.txt",header=TRUE) #讀取Example.txt,並存成"Data"這個物件(資料表格式)
Color="red" #指定顏色
pdf("plot.pdf") #開啟一個pdf file,路徑為"plot.pdf"
plot(Data,col=Color) #將圖畫到開啟中的pdf file上去
dev.off() #關掉這個pdf file
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Ploting your data"),
sidebarPanel(
fileInput(inputId="files", label=h4("Upload your data file:"), multiple=FALSE, accept="text/plain"),
helpText("Note: you only can upload the .txt file."),
radioButtons("Color", "Select the color of histogram:", choices = c("Red" = "red", "Blue" = "blue", "Green" = "green"))
),
mainPanel(
plotOutput("scatterPlot"),
downloadButton("download", label = "Download plot", class = NULL)
)
))
library(shiny)
shinyServer(function(input, output) {
DATA <- reactive({
if (is.null(input$files)) {return()} else {
dat <- read.table(input$files$datapath,header=T)
return(dat)
}
})
output$scatterPlot = renderPlot({
dat = DATA()
if (is.null(dat)) {return()} else {
plot(dat,col=input$Color)
}
})
output$download <- downloadHandler(
filename = function() {'plot.pdf'},
content = function(con) {
dat = DATA()
if (is.null(dat)) {return()} else {
pdf(con)
plot(dat,col=input$Color)
dev.off()
}
}
)
})
–- 請點這裡下載測試檔案
test1=read.table("ttest.txt",header=TRUE) #讀取ttest.txt,並存成"test1"這個物件(資料表格式)
head(test1,10) #先簡單看下這個資料的樣子
## Y X
## 1 12.457205 1
## 2 4.378212 0
## 3 7.254833 0
## 4 25.491177 1
## 5 16.693391 0
## 6 4.793828 0
## 7 2.417034 0
## 8 2.577331 1
## 9 4.722540 1
## 10 18.960041 0
summary(test1) #看"test1"這個資料表的所有變項的基本資訊
## Y X
## Min. : 0.6798 Min. :0.00
## 1st Qu.: 3.9954 1st Qu.:0.00
## Median : 6.7960 Median :0.00
## Mean : 17.2717 Mean :0.49
## 3rd Qu.: 18.5476 3rd Qu.:1.00
## Max. :329.2665 Max. :1.00
– 所以,我們可能要做一個Student t test,或是一個linear regression。
Result1=t.test(Y~X,data=test1,var.equal=TRUE) #Student t test
Result1
##
## Two Sample t-test
##
## data: Y by X
## t = -1.6801, df = 98, p-value = 0.09613
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -26.827668 2.228592
## sample estimates:
## mean in group 0 mean in group 1
## 11.24495 23.54449
Result2=lm(Y~X,data=test1) #linear regression test
Result2
##
## Call:
## lm(formula = Y ~ X, data = test1)
##
## Coefficients:
## (Intercept) X
## 11.24 12.30
summary(Result2)
##
## Call:
## lm(formula = Y ~ X, data = test1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -22.243 -14.218 -7.164 1.918 305.722
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 11.245 5.125 2.194 0.0306 *
## X 12.300 7.321 1.680 0.0961 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 36.6 on 98 degrees of freedom
## Multiple R-squared: 0.028, Adjusted R-squared: 0.01808
## F-statistic: 2.823 on 1 and 98 DF, p-value: 0.09613
test1_0=test1[test1$X==0,]
test1_1=test1[test1$X==1,]
par(mfrow=c(1,2))
hist(test1_0$Y,col="red",xlab="Y",main="Histogram of Group 0")
hist(test1_1$Y,col="blue",xlab="Y",main="Histogram of Group 1")
test1$logY=log(test1$Y)
head(test1,10)
## Y X logY
## 1 12.457205 1 2.5222992
## 2 4.378212 0 1.4766404
## 3 7.254833 0 1.9816678
## 4 25.491177 1 3.2383324
## 5 16.693391 0 2.8150129
## 6 4.793828 0 1.5673293
## 7 2.417034 0 0.8825410
## 8 2.577331 1 0.9467543
## 9 4.722540 1 1.5523468
## 10 18.960041 0 2.9423336
test1_0=test1[test1$X==0,]
test1_1=test1[test1$X==1,]
par(mfrow=c(1,2))
hist(test1_0$logY,col="red",xlab="log of Y",main="Histogram of Group 0")
hist(test1_1$logY,col="blue",xlab="log of Y",main="Histogram of Group 1")
Result1=t.test(logY~X,data=test1,var.equal=TRUE) #Student t test
Result1
##
## Two Sample t-test
##
## data: logY by X
## t = -2.4832, df = 98, p-value = 0.01472
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.9725206 -0.1085616
## sample estimates:
## mean in group 0 mean in group 1
## 1.850561 2.391102
Result2=lm(logY~X,data=test1) #linear regression test
Result2
##
## Call:
## lm(formula = logY ~ X, data = test1)
##
## Coefficients:
## (Intercept) X
## 1.8506 0.5405
summary(Result2)
##
## Call:
## lm(formula = logY ~ X, data = test1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.2365 -0.7328 -0.2274 0.7564 3.4058
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.8506 0.1524 12.145 <2e-16 ***
## X 0.5405 0.2177 2.483 0.0147 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.088 on 98 degrees of freedom
## Multiple R-squared: 0.0592, Adjusted R-squared: 0.0496
## F-statistic: 6.166 on 1 and 98 DF, p-value: 0.01472
Result3=wilcox.test(Y~X,data=test1)
Result3
##
## Wilcoxon rank sum test with continuity correction
##
## data: Y by X
## W = 895, p-value = 0.01465
## alternative hypothesis: true location shift is not equal to 0
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Inferential statistics for a binary variable and continuous variable."),
sidebarPanel(
fileInput(inputId="files", label=h4("Upload your data file:"), multiple=FALSE, accept="text/plain"),
helpText("Note: you only can upload the .txt file."),
radioButtons("method", "What is the method to analysis?", choices = c("Student t test" = "1", "Linear regression" = "2", "Wilcoxon test" = "3")),
checkboxInput("log", label = "log-transformation")
),
mainPanel(
verbatimTextOutput("view"),
plotOutput("distPlot")
)
))
library(shiny)
shinyServer(function(input, output) {
DATA <- reactive({
if (is.null(input$files)) {return()} else {
dat <- read.table(input$files$datapath,header=T)
dat$logY=log(dat$Y)
return(dat)
}
})
output$distPlot = renderPlot({
dat = DATA()
if (is.null(dat)) {return()} else {
dat_0=dat[dat$X==0,]
dat_1=dat[dat$X==1,]
par(mfrow=c(1,2))
if (input$log) {
hist(dat_0$logY,col="red",xlab="log of Y",main="Histogram of Group 0")
hist(dat_1$logY,col="blue",xlab="log of Y",main="Histogram of Group 1")
} else {
hist(dat_0$Y,col="red",xlab="Y",main="Histogram of Group 0")
hist(dat_1$Y,col="blue",xlab="Y",main="Histogram of Group 1")
}
}
})
output$view <- renderPrint({
dat = DATA()
if (is.null(dat)) {return()} else {
x = dat$X
if (input$log) {y = dat$logY} else {y = dat$Y}
if (input$method=="1") {t.test(y~x,var.equal=TRUE)}
else if (input$method=="2") {summary(lm(y~x))}
else {wilcox.test(y~x)}
}
})
})